﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonsLib.DB;
using MaxLeaf.Platform.DALImplementation;
using System.Data;
using MaxLeaf.Platform.Functions;
using CommonsLib.Utility;

namespace MaxLeaf.Platform.BRL
{
    public class BaseBRL
    {
        public  virtual bool AddValid(DBAccess dbcon, BaseDAL dal, object model)
        {
            if (!IsNotExist(dbcon,   model))
            {
                return false;
            }
            if (!Unique(dbcon, dal, model))
            {
                return false;
            }
            return true;
        }

        public virtual bool Unique(DBAccess dbcon, BaseDAL dal, object model)
        {
            if( string.IsNullOrEmpty( this.UniqueFields)) return true;
            string[] arrFields = this.UniqueFields.Split('|');
            foreach (string strFields in arrFields)
            {
                string[] arrField = strFields.Split(',');
                if (!dal.CheckUnique(dbcon, model, arrField))
                {
                    string strFieldValue=string.Empty;
                    foreach (string strField in arrField)
                    {
                        strFieldValue +=string.Format( "{0}: {1} ",strField,DataFunc.GetValue(model,strField));
                    }
                    throw new MaxLeaf.Platform.Commons.MaxLeafNotUniqueException(string.Format("{0} 已存在", strFieldValue));
                }
            }


            return true;
        }



        public virtual bool UpdateValid(DBAccess dbcon, BaseDAL dal, object model)
        {
            if (!IsExistedAndNotChanged(dbcon,  model))
            {
                return false;
            }
            if (!Unique(dbcon, dal, model))
            {
                return false;
            }
            return true;
        }

        public virtual bool DeleteValid(DBAccess dbcon, BaseDAL dal, object model)
        {
            if (!IsExistedAndNotChanged(dbcon,   model))
            {
                return false;
            }
            return true;
        }

        public bool IsNotExist(DBAccess dbcon, object model)
        {

            if (DataFunc.CheckRecordExists(dbcon, model))
            {
                return false;
            }

            return true;
            
        }

        public bool IsExistedAndNotChanged(DBAccess dbcon, object model)
        {
            bool bReturn = true;
            byte[] btDBRecStamp;
            byte[] btRecStamp;

            bReturn = IsNotExist(dbcon, model);
            if (bReturn)
            {
                throw new MaxLeaf.Platform.Commons.MaxLeafException("The record is not on file!");

            }
            else
            {
                bReturn = true;
                if (DataFunc.ExistField(model, "recstamp"))
                {

                    btDBRecStamp = (byte[])DataFunc.GetDBValue(dbcon, model, "recstamp");
                    btRecStamp = (byte[])DataFunc.GetValue( model, "recstamp");

                    if (btRecStamp.Length == btDBRecStamp.Length)
                    {
                        for (int i = 0; i < btRecStamp.Length; i++)
                        {
                            if (btRecStamp[i] != btDBRecStamp[i])
                            {
                                bReturn = false;
                                break;
                            }
                        }
                    }
                    else
                    {
                        bReturn = false;
                    }

                    if (!bReturn)
                    {
                        throw new MaxLeaf.Platform.Commons.MaxLeafException(string.Format("记录已被其他人修改 {0}!", model));

                    }
                }

            }

            return bReturn;
        }

        /// <summary>
        /// 需要验证唯一性的字段
        /// </summary>
        public virtual string UniqueFields
        {
            get
            {
                return string.Empty;
            }
        }

        public virtual bool CanPost(DBAccess dbcon, BaseDAL dal,string strTableName,string strKeyName, int intKey )
        {
            return dal.CheckCanPost(dbcon, strTableName, strKeyName, intKey);
               
             
        }
    }
}
